package com.tjrac.generalpermissions.dao;

import com.tjrac.generalpermissions.domain.PType;
import com.tjrac.generalpermissions.domain.Permission;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.security.Permissions;
import java.util.List;

/**
 * PermissionDao
 *
 * @author : xziying
 * @create : 2020-11-22 14:34
 */
@Repository
public interface PermissionDao {

    /**
     * 根据ptid查询所有该类型的权限
     */
    @Select("SELECT * FROM permission WHERE `ptid` = #{id}")
    List<Permission> queryByPTid(int id);

    @Select("select * from permission")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "desc", property = "desc"),
            @Result(column = "link", property = "link"),
            @Result(column = "ptid", property = "ptid")
    })
    List<Permission> queryAll();

    /**
     * 根据角色id查询所拥有的权限
     */
    @Select("SELECT * FROM `rolepermission` LEFT JOIN `permission` ON rolepermission.pid = permission.id WHERE rid=#{rid}")
    @Results({
            @Result(column = "pid", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "desc", property = "desc"),
            @Result(column = "link", property = "link"),
            @Result(column = "ptid", property = "ptid")
    })
    List<Permission> queryByRid(Integer rid);

    @Select("select * from permission where id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "desc", property = "desc"),
            @Result(column = "link", property = "link"),
            @Result(column = "ptid", property = "ptid")
    })
    Permission queryById(int id);

    @Select("select * from permission where `name` = #{name}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "desc", property = "desc"),
            @Result(column = "link", property = "link"),
            @Result(column = "ptid", property = "ptid")
    })
    Permission queryByName(String name);

    @Insert("insert into permission(`name`, `desc`, link, ptid) values(#{name}, #{desc}, #{link}, #{ptid})")
    int addPermission(Permission permission);

    @Delete("delete from permission where `id` = #{id}")
    int delById(int id);

    @Update("update permission set `name` = #{name}, `desc` = #{desc}, `link` = #{link}, `ptid` = #{ptid} where `id` = #{id}")
    int modifyById(Permission permission);

}
